package org.zzh.controller.system.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.zzh.controller.BaseController;
import org.zzh.dao.common.PageBean;
import org.zzh.domain.system.dept.PeDept;
import org.zzh.domain.system.role.PeRole;
import org.zzh.domain.system.user.PeUser;
import org.zzh.service.common.CommonService;
import org.zzh.service.system.dept.PeDeptService;
import org.zzh.service.system.role.PeRoleService;
import org.zzh.service.system.user.PeUserService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/user")
public class PeUserController extends BaseController {

    @Autowired
    private PeUserService peUserService;

    @Autowired
    private PeDeptService peDeptService;

    @Autowired
    private PeRoleService roleService;

    @RequestMapping(value = "/list.do")
    public ModelAndView list(
            @RequestParam(required = false, value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(required = false, value = "name", defaultValue = "") String name
    ) {
        PageBean<PeUser> list = peUserService.list(page, pageSize, name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageBean", list);
        modelAndView.addObject("name", name);
        modelAndView.setViewName("/WEB-INF/pages/system/user/user-list");
        return modelAndView;
    }

    @RequestMapping (value = "/toAdd.do")
    public ModelAndView toAdd() {
        ModelAndView modelAndView = new ModelAndView();
        List<PeDept> deptList = peDeptService.findParentList();
        modelAndView.addObject("deptList", deptList);
        modelAndView.setViewName("/WEB-INF/pages/system/user/user-add");
        return modelAndView;
    }


    @RequestMapping (value = "/toUpdate.do")
    public ModelAndView toUpdate(String id) {
        ModelAndView modelAndView = new ModelAndView();
        PeUser user = peUserService.findById(id);
        List<PeDept> deptList = peDeptService.findParentList();
        modelAndView.addObject("user", user);
        modelAndView.addObject("deptList", deptList);
        modelAndView.setViewName("/WEB-INF/pages/system/user/user-update");
        return modelAndView;
    }

    @RequestMapping (value = "/edit.do")
    public String edit(PeUser user) {
        if (user.getUserId() == null){
            PeDept peDept = (PeDept)session.getAttribute("company");
            if (peDept == null){
                user.setCompanyId(companyId);
                user.setCompanyName(companyName);
            }
            else {
                user.setCompanyId(peDept.getDeptId());
                user.setCompanyName(peDept.getDeptName());
            }
            user.setUserId(UUID.randomUUID().toString());
            peUserService.add(user);
            return "redirect:/system/user/list.do";
        }
        peUserService.update(user);
        return "redirect:/system/user/list.do";
    }


    @RequestMapping (value = "/delete.do")
    public String delete(String[] ids) {
        peUserService.deleteList(ids);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping (value = "/roleList.do")
    public ModelAndView roleList(String id) {
        ModelAndView modelAndView = new ModelAndView();
        PeUser user = peUserService.findById(id);
        List<String> userRoleIds = roleService.getUserRoleIds(id);
        List <PeRole> roleList = roleService.list();
        String userRoleStr = userRoleIds.toString();
        modelAndView.addObject("userRoleStr", userRoleStr);
        modelAndView.addObject("roleList", roleList);
        modelAndView.addObject("user", user);
        modelAndView.setViewName("/WEB-INF/pages/system/user/user-role");
        return modelAndView;
    }


    @RequestMapping (value = "/changeRole.do")
    public String changeRole(String[] roleIds, String userId) {
        peUserService.changeRole(roleIds, userId);
        return "redirect:/system/user/list.do";
    }
}
